<template>
  <div class="content-page">
    <div class="content-nav">
      <el-breadcrumb class="breadcrumb" separator="/">
        <el-breadcrumb-item :to="{ name: 'dashboard' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>数据管理</el-breadcrumb-item>
        <el-breadcrumb-item>备份与还原</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="content-main">
      <div class="form-table-box">
        <el-upload class="upload" ref="upload" action="customize" :http-request="uploadFile" :file-list="fileList" :auto-upload="false"
                   :on-remove="handleRemove" :on-preview="handlePreview" :on-change="handleChange" :loading="uploadLoading">
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
          <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">还原数据</el-button>
          <div slot="tip" class="el-upload__tip">上传备份的zip文件</div>
        </el-upload>
        <el-button :loading="downloadLoading" style="margin-bottom:20px;margin-top:100px;" type="primary" icon="document"
                   @click="handleDownload">备份数据</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {exportObjToJsonZip} from '@/vendor/Export2Zip'
import JSZip from 'jszip'
export default {
  data () {
    return {
      downloadLoading: false,
      uploadLoading: false,
      fileList: []
    }
  },
  methods: {
    goBackPage () {
      this.$router.go(-1)
    },
    submitUpload () {
      this.$refs.upload.submit()
    },
    uploadFile (params) {
      const txtName = 'kumeng_data.json'
      const zip = new JSZip()
      zip.loadAsync(params.file).then(file => {
        zip.file(txtName).async('string').then(content => {
          const objData = JSON.parse(content)
          this.removeAllData().then(result => {
            console.log('kumeng: ' + result)
            this.restoreAllData(objData).then(result => {
              console.log('kumeng: ' + result)
              this.$message({
                type: 'success',
                message: '数据恢复成功'
              })
              this.uploadLoading = true
            })
          })
        })
      })
    },
    handleRemove (file, fileList) {
      console.log(file, fileList)
    },
    handlePreview (file) {
      console.log(file)
    },
    handleChange (file, fileList) {
      this.fileList = fileList.slice(-1)
    },
    handleDownload () {
      this.downloadLoading = true
      this.getAllData().then(obj => {
        exportObjToJsonZip(obj, 'kumeng_data', 'kumeng_data')
        this.downloadLoading = false
      })
    },
    async getAllData () {
      let datas = {}
      await this.$db.users.persistence.compactDatafile()
      datas.users = await this.$db.users.asyncFind({})
      datas.records = await this.$db.records.asyncFind({})
      datas.category = await this.$db.category.asyncFind({})
      return datas
    },
    async restoreAllData (objData) {
      await this.$db.users.asyncInsert(objData.users)
      await this.$db.records.asyncInsert(objData.records)
      await this.$db.category.asyncInsert(objData.category)
      return 'restoreSuccess'
    },
    async removeAllData () {
      await this.$db.users.asyncRemove({})
      await this.$db.records.asyncRemove({})
      await this.$db.category.asyncRemove({})
      await this.$db.users.persistence.compactDatafile()
      return 'removeSuccess'
    }
  },
  name: 'BackupPage'
}
</script>

<style scoped>

</style>
